package com.yequan.datastructure.algorithm.recursion;

/**
 * N的阶乘
 * 数学归纳法:
 * 当n=1时, 1! = 1
 * 当n=2时, 2! = 2 * 1 = 2 * 1!
 * 当n=3时, 3! = 3 * 2 * 1 = 3 * 2!
 * 当n=4时, 4! = 4 * 3 * 2 * 1 = 4 * 3!
 * 当n=n使, n! = n * (n-1) * (n-2) * ... * 1 = n * (n-1)!
 * 假设求解n!的函数为 recursion(n) 即 n!=recursion(n)
 * 则根据以上推论得出递推公式为 recursion(n) = n * recursion(n-1)
 *
 * @author : Administrator
 * @date : 2020/3/14
 */
public class Factorial {

    public static void main(String[] args) {
        Factorial factorial = new Factorial();
        int factorialSum = factorial.factorial(4);
        System.out.println(factorialSum);
    }

    public int factorial(int n) {
        if (n == 1) {
            return 1;
        }
        return n * factorial(n - 1);
    }

}
